﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;
namespace MBSBusinessEntities
{
   public class UserBE : ICrud
   {
        #region Constructor
       public UserBE(int userID)
       {
           this.userID = userID;
           mbs = new MBS_Entities();
       }
       public UserBE()
       {
           mbs = new MBS_Entities();
       }
        public UserBE(string emailID, string password)
        {
            this.emailID = emailID;
            this.password = password;
            mbs = new MBS_Entities();
        }
        public UserBE(string role, string userName, string password, bool isDeleted, long contact, string emailID, int rewardPoints, string preferences, string userNotified)
        {
            this.Role = role;
            this.UserName = userName;
            this.Password = password;
            this.IsDeleted = isDeleted;
            this.Contact = contact;
            this.EmailID = emailID;
            this.RewardPoints = rewardPoints;
            this.Preferences = preferences;
            this.UserNotified = userNotified;
            mbs = new MBS_Entities();
        }

        public UserBE(string name, string emailID, string password, int mobileNo, bool notifyMe)
       {
           this.emailID = emailID;
           this.userName = name;
           this.password = password;
           this.Contact = mobileNo;
           //userNotified column has to be changed in bool...
          // this.userNotified = notifyMe;
            mbs = new MBS_Entities();
       }
        #endregion

        #region ClassVariables

        public string emailID;
        public string password;
        private MBS_Entities mbs;
        private int userID;
        private string role;
        private string userName;
        private bool isDeleted;
        private long contact;
        private int rewardPoints;
        private string preferences;
        private string userNotified;
        
        #endregion


        #region Properties
        public int UserID
        {
            get { return userID; }

        }
        public string Role
        {
            get { return role; }
            set { role = value; }
        }
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public bool IsDeleted
        {
            get { return isDeleted; }
            set { isDeleted = value; }
        }


        public long Contact
        {
            get { return contact; }
            set { contact = value; }
        }


        public string EmailID
        {
            get { return emailID; }
            set { emailID = value; }
        }


        public int RewardPoints
        {
            get { return rewardPoints; }
            set { rewardPoints = value; }
        }


        public string Preferences
        {
            get { return preferences; }
            set { preferences = value; }
        }


        public string UserNotified
        {
            get { return userNotified; }
            set { userNotified = value; }
        }
        #endregion


        #region Methods
        private UserBE ConvertDALToBusinessEntity(User userDAL)
        {

            UserBE userbe = new UserBE();
            userbe.userID = userDAL.UserID;
            userbe.Role = userDAL.Role;
            userbe.UserName = userDAL.UserName;
            userbe.Password = userDAL.Password;
            // userbe.IsDeleted = userDAL.IsDeleted;
            userbe.Contact = (long)userDAL.Contact;
            userbe.EmailID = userDAL.EmailID;
            userbe.RewardPoints = (int)userDAL.TotalRewardPoints;
            userbe.Preferences = this.Preferences;
            userbe.UserNotified = this.UserNotified;
            return userbe;
        }
        private User GetUserDALByID(int UserID)
        {
            User user = new User();
            try
            {
                user = mbs.Users.First(x => x.UserID == UserID);
            }
            catch (System.InvalidOperationException ex)
            {
                return null;
            }
            return user;
        }

        public int GetUserIDByEmail(string emailID)
        {
            User user = new User();
            try
            {
                user = mbs.Users.First(x => x.EmailID == emailID);
            }
            catch (System.InvalidOperationException ex)
            {
                return 0;
            }
            return user.UserID;
        }
        public bool authenticate()
        {
            string email = this.emailID;
            string pwd = this.password;
            
            User user = mbs.Users.FirstOrDefault(x => x.EmailID == email && x.Password==pwd);
            if (user != null)
            {
                return true;
            }
            return false;
        }

        public bool AddRewardsPoint(int points)
        {
            User user = mbs.Users.FirstOrDefault(x => x.UserID == this.userID);
            user.TotalRewardPoints += points;
            int num = mbs.SaveChanges();
            return num > 0;
        }

        public bool WithDrawRewardsPoint(int points)
        {
            User user = mbs.Users.FirstOrDefault(x => x.UserID == this.userID);
            user.TotalRewardPoints -= points;
            int num = mbs.SaveChanges();
            return num > 0;
        }
        #endregion

         #region ICrud
          public bool Add()
        {
            User user = new User();
            try
            {
                user.Role = this.Role;
                user.UserName = this.UserName;
                user.Password = this.Password;
               // user.IsDeleted = false;
                user.Contact = this.Contact;
                user.EmailID = this.EmailID;
                user.TotalRewardPoints = this.RewardPoints;
                user.Preferences = this.Preferences;
                user.UserNotified = this.UserNotified;
                mbs.AddToUsers(user);
                mbs.SaveChanges();

            }
            catch(System.Data.UpdateException ex)
            {
                return false;
            }
            return true;
            
        }

        public bool Delete(int UserID)
        {
            User user = GetUserDALByID(UserID);
            if(user==null)
            {
                return false;
            }
            //user.IsDeleted = true;
            mbs.SaveChanges();
            return true;
        }

       

        public bool Update()
        {
            User user = GetUserDALByID(UserID);
            user.Role = this.Role;
            user.UserName = this.UserName;
            user.Password = this.Password;
            // user.IsDeleted = false;
            user.Contact = this.Contact;
            user.EmailID = this.EmailID;
            user.TotalRewardPoints = this.RewardPoints;
            user.Preferences = this.Preferences;
            user.UserNotified = this.UserNotified;
            mbs.SaveChanges();
            return true;

        }
        
        public object GetByID(int UserID)
        {
            UserBE userbe = new UserBE();
            User user =  GetUserDALByID(UserID);
            userbe = ConvertDALToBusinessEntity(user);
            return userbe;
        }

        public bool UpdateRewardPoints(int AdditionalRewardPoints)
        {
            User user = GetUserDALByID(this.UserID);
            user.TotalRewardPoints = user.TotalRewardPoints + AdditionalRewardPoints;
            
            mbs.SaveChanges();

            return true;
        }
          #endregion

        private bool isValid = true;

        public bool IsValid
        {
            get { return isValid; }
            set { isValid = value; }
        }

        

    }
  }

